<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./css/layui.css">
  <script src="./layui.js"></script>
</head>

<body>
  <div id="test12" class="demo-tree-more"></div>
  <div id="test13" class="demo-tree-more"></div>
  <script src="./data.js"></script>
  <script>
    // let treeNode = {}
    const obj = clone(dataObj)
    let departmentNode = []
    formatDepartmentData(obj.innerList)
    let companyNode = formatCompanyData(obj.innerList, 'company')
    // departmentNode = formatDepartmentData2(departmentNode)
    departmentNode.forEach((val, idx) => {
      departmentNode[idx] = formatDepartmentData2(val)
    })
    function formatCompanyData(data, type) {
      Object.keys(data).map(k => {
        if (data[k].type == type) {
          data[k].title = data[k].name
          if (data[k].hasOwnProperty('children') && Object.keys(data[k].children).length > 0) {
            formatCompanyData(data[k].children, type)
            data[k].children = Object.values(data[k].children)
          }
        } else {
          delete data[k]
        }
      })
      return Object.values(data)
    }

    function formatDepartmentData2(data, type) {
      Object.keys(data).map(arr => {
        if (arr.type == type) {
          arr.title = arr.name
          if (arr.hasOwnProperty('children') && Object.keys(arr.children).length > 0) {
            formatCompanyData(arr.children, type)
            arr.children = Object.values(arr.children)
          }
        }
      })

      // if (data.level == 0) {
      //   return data
      // } else {
      return Object.values(data)
      // }
    }

    function clone(obj) {
      return JSON.parse(JSON.stringify(obj))
    }



    function formatDepartmentData(data) {
      Object.keys(data).map(k => {
        if (data[k].type === 'department') {
          data[k].title = data[k].name

          departmentNode.push(data[k])
        } else {
          if (data[k].hasOwnProperty('children') && Object.keys(data[k].children).length > 0) {
            formatDepartmentData(data[k].children)
            data[k].children = Object.values(data[k].children)
          }
        }
      })
    }

    console.log(dataObj)

    layui.use(['tree', 'util'], function () {
      var tree = layui.tree
        , layer = layui.layer
        , util = layui.util


      //基本演示
      tree.render({
        elem: '#test12'
        , data: companyNode
        , showCheckbox: true  //是否显示复选框
        , id: 'demoId1'
        , isJump: true //是否允许点击节点时弹出新窗口跳转
        , click: function (obj) {
          var data = obj.data;  //获取当前点击的节点数据
          layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
        }
      });

      //基本演示
      tree.render({
        elem: '#test13'
        , data: departmentNode
        , showCheckbox: true  //是否显示复选框
        , id: 'demoId1'
        , isJump: true //是否允许点击节点时弹出新窗口跳转
        , click: function (obj) {
          var data = obj.data;  //获取当前点击的节点数据
          layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
        }
      });

      //按钮事件
      util.event('lay-demo', {
        getChecked: function (othis) {
          var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据

          layer.alert(JSON.stringify(checkedData), { shade: 0 });
          console.log(checkedData);
        }
        , setChecked: function () {
          tree.setChecked('demoId1', [12, 16]); //勾选指定节点
        }
        , reload: function () {
          //重载实例
          tree.reload('demoId1', {

          });

        }
      });
    });

  </script>
</body>

</html>